package com.styytech.dadabao.widge.clippicture;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 定义了一些公用常量
 */
public abstract class ConstantsActivity extends Activity {
    /**
     * 获取拍照后照片
     */
    protected static final int PIC_TAKE_FROM_CAMERA = 0;
    /**
     * 获取相册照片
     */
    protected static final int PIC_GET_FROM_CAMERA = 1;
    /**
     * 获取拍照后照片
     */
    protected static final int PIC_REQUEST_FROM_CAMERA = 2;
    /**
     * 从本地相册获取照片
     */
    protected static final int PIC_REQUEST_FROM_LOCAL = 3;
    /**
     * 截图功能界面
     */
    protected static final int PIC_REQUEST_CLIPPICTURE = 4;
    /**
     * 拍照照片存储
     */
    protected static final int CAMERA_REQUEST_CODE = 5;
    /**
     * 裁剪图片
     */
    protected static final int IMAGE_REQUEST_CODE = 6;
    /**
     * 裁剪之后的图片数据
     */
    protected static final int RESULT_REQUEST_CODE = 7;

    /**
     * 存储获取照片的临时目录
     * 定义static类型，避免不同子类继承该父类时，该成员变量被回收
     */
    private static String photoTempPath = Environment
            .getExternalStorageDirectory().getAbsolutePath()
            + File.separator
            + "Dada"
            + File.separator + "images" + File.separator + "headImg.jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * 设置存储获取照片的临时目录
     * 默认是路径是sd卡根目录dada/images/headImg.jpg
     *
     * @param photoTempPath
     */
    public void setPhotoTempPath(String photoTempPath) {
        if (photoTempPath != null) {
            this.photoTempPath = photoTempPath;
        }
    }

    /**
     * 获取存储获取照片的临时目录
     * 默认是路径是sd卡根目录dada/images/headImg.jpg
     *
     * @return photoTempPath
     */
    public String getPhotoTempPath() {
        return photoTempPath;
    }

    /**
     * 保存图片到指定路径
     *
     * @param bm      bitmap
     * @param quality 压缩比例
     */
    public void saveBitmap(Bitmap bm, int quality) {
        String path = getPhotoTempPath();
        File f = new File(path);
        if (f.exists()) {
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.JPEG, quality, out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
